home *** CD-ROM | disk | FTP | other *** search
/ Aminet 37 / Aminet 37 (2000)(Schatztruhe)[!][Jun 2000].iso / Aminet / dev / lang / sofa.lha / sofa / smalleiffel / lib_number / abstract_integer.e < prev    next >
Text File  |  2000-03-25  |  4KB  |  165 lines

  1. -- This file is  free  software, which  comes  along  with  SmallEiffel. This
  2. -- software  is  distributed  in the hope that it will be useful, but WITHOUT 
  3. -- ANY  WARRANTY;  without  even  the  implied warranty of MERCHANTABILITY or
  4. -- FITNESS  FOR A PARTICULAR PURPOSE. You can modify it as you want, provided
  5. -- this header is kept unaltered, and a notification of the changes is added.
  6. -- You  are  allowed  to  redistribute  it and sell it, alone or as a part of 
  7. -- another product.
  8. --          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
  9. --            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr 
  10. --                       http://SmallEiffel.loria.fr
  11. --
  12. deferred class ABSTRACT_INTEGER
  13. --
  14. -- To implement NUMBER (do not use this class, see NUMBER).
  15. --
  16.    
  17. inherit NUMBER   
  18.    
  19. feature 
  20.    
  21.    append_in(str: STRING) is
  22.      -- Append the equivalent of `to_string' at the end of 
  23.      -- `str'. Thus you can save memory because no other
  24.      -- STRING is allocate for the job.
  25.       local
  26.      val: NUMBER;
  27.      i, j: INTEGER;
  28.       do
  29.      if is_zero then
  30.         str.extend('0');
  31.      else
  32.         if is_negative then
  33.            str.extend('-');
  34.         end;
  35.         from
  36.            i := str.count + 1;
  37.            val := Current.abs;
  38.         until
  39.            val.is_zero
  40.         loop
  41.            str.extend((val @\\ 10).digit);
  42.            val := val @// 10;
  43.         end;
  44.         from  
  45.            j := str.count;
  46.         until
  47.            i >= j
  48.         loop
  49.            str.swap(i,j);
  50.            j := j - 1;
  51.            i := i + 1 ;
  52.         end;
  53.      end;
  54.       end; 
  55.    
  56. feature {NUMBER}
  57.    
  58.    integer_divide_small_integer(other: SMALL_INTEGER): ABSTRACT_INTEGER is
  59.       require
  60.      other /= Void;
  61.       deferred      
  62.       ensure
  63.      Result /= Void;
  64.       end;
  65.       
  66.    remainder_of_divide_small_integer(other: SMALL_INTEGER): ABSTRACT_INTEGER is
  67.       require
  68.      other /= Void;
  69.       deferred      
  70.       ensure
  71.      Result /= Void;
  72.       end;
  73.    
  74.    
  75.    integer_divide_large_positive_integer(other: LARGE_POSITIVE_INTEGER): ABSTRACT_INTEGER is
  76.       require
  77.      other /= Void;
  78.       deferred
  79.       ensure
  80.      Result /= Void;
  81.       end;
  82.    
  83.    remainder_of_divide_large_positive_integer(other: LARGE_POSITIVE_INTEGER): ABSTRACT_INTEGER is
  84.       require
  85.      other /= Void;
  86.       deferred
  87.       ensure
  88.      Result /= Void;
  89.       end;
  90.    
  91.    
  92.    integer_divide_large_negative_integer(other: LARGE_NEGATIVE_INTEGER): ABSTRACT_INTEGER is
  93.       require
  94.      other /= Void;
  95.       deferred 
  96.       ensure
  97.      Result /= Void;
  98.       end;
  99.    
  100.    remainder_of_divide_large_negative_integer(other: LARGE_NEGATIVE_INTEGER): ABSTRACT_INTEGER is
  101.       require
  102.      other /= Void;
  103.       deferred 
  104.       ensure
  105.      Result /= Void;
  106.       end;
  107.    
  108.    
  109. feature{NONE}
  110.    
  111.    mult_2_integer(a, b: INTEGER) is
  112.      -- multiply a and b and store the result in temp_2_digints
  113.       require
  114.      a >= 0; 
  115.      b >= 0;
  116.       local
  117.      left_a, left_b, right_a, right_b: INTEGER;
  118.      upper_half_base: INTEGER;
  119.      temp1, temp2: INTEGER;
  120.       do
  121.      upper_half_base := 2*Half_base;
  122.      left_a := a // upper_half_base;
  123.      right_a := a \\ upper_half_base;
  124.      left_b := b // Half_base;
  125.      right_b := b \\ Half_base;
  126.      
  127.      temp_2_digints.put(left_a * left_b, 1);
  128.      temp_2_digints.put(right_a * right_b, 0);
  129.      
  130.      temp1 := left_a * right_b;
  131.      temp_2_digints.put((temp_2_digints @ 1) + (temp1 // Half_base), 1);
  132.      temp2 := (temp1 \\ Half_base)*upper_half_base + (temp_2_digints @ 0);
  133.      if (temp2 < 0) then
  134.         temp_2_digints.put(temp2 - Base, 0);
  135.         temp_2_digints.put((temp_2_digints @ 1) + 1, 1);
  136.      else
  137.         temp_2_digints.put(temp2 , 0);
  138.      end;
  139.      
  140.      temp1 := left_b * right_a;
  141.      if (temp1 < 0) then
  142.         temp1 := temp1 - Base;
  143.         temp_2_digints.put((temp_2_digints @ 1) + Half_base, 1);
  144.      end;
  145.      temp_2_digints.put((temp_2_digints @ 1)+(temp1 // upper_half_base), 1);
  146.      temp2 := (temp1 \\ upper_half_base)*Half_base + (temp_2_digints @ 0);
  147.      if (temp2 < 0) then
  148.         temp_2_digints.put(temp2 - Base, 0);
  149.         temp_2_digints.put((temp_2_digints @ 1) + 1, 1);
  150.      else
  151.         temp_2_digints.put(temp2, 0);
  152.      end;
  153.       end;
  154.       
  155. end -- ABSTRACT_INTEGER
  156.  
  157.  
  158.  
  159.  
  160.  
  161.  
  162.  
  163.  
  164.  
  165.